package com.example.orderserver.controller;

import com.alibaba.fastjson.JSON;
import com.example.orderserver.feign.ProductFeign;
import com.example.orderserver.feign.ProductFeignNacos;
import com.example.productserverapi.entity.Product;
import com.netflix.client.ClientException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author benjamin_5
 * @Description
 * @date 2023/2/14
 */
@RestController
@RequestMapping("order")
@RefreshScope
public class OrderController {

    @Value("${server.port}")
    private String port;

    @Value("${cloud7day.wu}")
    private String test;

    @Value("${common.name}")
    private String name;

    @Autowired
    private ProductFeign productFeign;
    @Autowired
    @Qualifier("product-nacos")
    private ProductFeignNacos productFeignNacos;

    @GetMapping("get")
    public String getOrder() throws InterruptedException {
        return "order:"+port;
    }

    @PostMapping("post")
    public String postOrder(){
        return "post order: "+ port;
    }

    @GetMapping("test")
    public String getTest(){
        return String.format("test: %s, port: %s", test, port);
    }

    @GetMapping("common")
    public String getCommon(){
        return "common: " + name;
    }

    @GetMapping("getAll")
    public String getAll(){
        return "order info " + productFeign.getInfo();
    }

    @GetMapping("getAll2")
    public String getAll2() throws ClientException {
        return "order info " + productFeignNacos.getInfo();
    }

    @GetMapping("getAll3")
    public String getAll3(String name){
        return "order info " + productFeignNacos.getByName(name);
    }

    @GetMapping("getBody")
    public String getBody(){
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        return "order info " + productFeignNacos.getByBody(product);
    }

    @GetMapping("getBody2")
    public String getBody2(){
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        // 实体类转json字符串 需要引入fastjson依赖
        String jsonString = JSON.toJSONString(product);
        // json字符串转map
        Map params = JSON.parseObject(jsonString, Map.class);
        return "order info " + productFeignNacos.getByBody2(params);
    }

    @GetMapping("getBody3")
    public String getBody3(){
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        return "order info " + productFeignNacos.getByBody3(product);
    }

    @GetMapping("getBody4")
    public String getBody4(){
        Product product = new Product();
        product.setId(1L);
        product.setName("苹果");
        product.setPrice(new BigDecimal("123.3"));
        product.setQuantity(2);
        return "order info " + productFeignNacos.getByBody4(product);
    }
}
